# Importing Data
JAPAN <- read_excel("C:/Users/Biniam/Desktop/Documents/Academic/Thesis/Analysis Folder/Excel Files/UAE/Japan.xlsx")

# Checking the Imported Data
View(JAPAN)

# Creating Time Series Data
JAPAN_ts <- ts(JAPAN, start=c(2001,01), end=c(2019,06), frequency=12)

# Viewing and Checking the Created Time Series Data
JAPAN_ts
sum(is.na(JAPAN_ts))
library(forecast)
JAPAN_ts <- tsclean(JAPAN_ts)

# Step – 1 of the Box-Jenkins Methodology (Identification: Plotting the Time Series Data)
plot(JAPAN_ts, cex = 0.5, cex.main = 1.5, font.main = 4, font.lab = 2, cex.lab = 1.5, cex.axis = 0.1, col = 2, col.main = 4, col.lab = 2, col.axis = 3, xlab = "Years", ylab = "Volume of Vehicles (in Units)", main = "Monthly Time Series Data for Vehicles Exported from Japan to UAE")
plot(aggregate(JAPAN_ts,FUN=mean))

# Decomposing
JAPAN_ts_decomp <- decompose(JAPAN_ts)
plot(JAPAN_ts_decomp)

# Testing for Stationarity
acf(JAPAN_ts, lag.max=20)
pacf(JAPAN_ts, lag.max=20)

# To see any seasonal effect
boxplot(JAPAN_ts~cycle(JAPAN_ts))

# To remove trend effect
JAPAN_ts_diff <- diff(JAPAN_ts)
plot(JAPAN_ts_diff)

# To remove variance effect
JAPAN_ts_log <- log(JAPAN_ts)
plot(JAPAN_ts_log)

# To remove both (Trend and Variance) effects
JAPAN_ts_both <- diff(log(JAPAN_ts))
plot(JAPAN_ts_both)


# Dealing with ACF and PACF
install.packages("tseries")
library(tseries)
acf(JAPAN_ts, lag.max=20)
acf(log(JAPAN_ts), lag.max=20)
acf(diff(JAPAN_ts), lag.max=20)
acf(diff(log(JAPAN_ts)), lag.max=20)
pacf(JAPAN_ts, lag.max=20)
pacf(log(JAPAN_ts), lag.max=20)
pacf(diff(JAPAN_ts), lag.max=20)
pacf(diff(log(JAPAN_ts)), lag.max=20)

# Step-2 of the Box-Jenkins Methodology (Estimating the appropriate model)
JAPAN_ts_model <- auto.arima(JAPAN_ts)
JAPAN_ts_model
JAPAN_ts_model <- auto.arima(JAPAN_ts, ic="aic", trace = TRUE)
JAPAN_ts_model

# Step-3 of the Box-Jenkins Methodology (Diagnosis Checking)
library(tseries)
plot.ts(JAPAN_ts_model$resid, cex = 0.5, cex.main = 1.5, font.main = 4, font.lab = 2, cex.lab = 1.5, cex.axis = 0.75, col = 2, col.main = 4, col.lab = 2, col.axis = 4, xlab = "Years", ylab = "Residuals", main = "Residual Plot")
acf(JAPAN_ts_model$residuals, cex = 0.5, cex.main = 1.5, font.main = 4, font.lab = 2, cex.lab = 1.5, cex.axis = 0.75, col = 2, col.main = 4, col.lab = 1, col.axis = 4, xlab = "Lag", ylab = "ACF", main="ACF Residual")
pacf(JAPAN_ts_model$residuals, main='ACF Residual')
Box.test(JAPAN_ts_model$resid, lag=20, type="Ljung-Box")

# Forecasting
options(max.print=1000000)
library(forecast)
JAPAN_ts_forecast <- forecast (JAPAN_ts_model, level=c(95), h=258)
plot(JAPAN_ts_forecast, cex = 0.5, cex.main = 1.5, font.main = 4, font.lab = 2, cex.lab = 1.5, cex.axis = 0.1, col = 2, col.main = 4, col.lab = 2, col.axis = 3, xlab = "Years", ylab = "Volume of Vehicles (in Units)", main = "Time Series Forecasting for Vehicles Exported from Japan to UAE")


JAPAN_ts_forecast             


write.table(JAPAN_ts_forecast, file="Japan_TSA.csv", sep=",")
